package cn.mibcxb.java.dolphin.gui;

import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.apache.commons.lang3.StringUtils;

import cn.mibcxb.java.dolphin.client.Dolphin;

public class Window {
	private Dolphin mContext = null;
	private String mName = null;
	private JFrame mMainFrame = null;
	private int mScnWidth;
	private int mScnHeight;
	private int mWidth = 480;
	private int mHeight = 600;
	private JTextArea mSessionTextArea;
	private JTextArea mTextArea;

	public Window(Dolphin context, String name) {
		super();
		this.mContext = context;
		this.mName = name;
		init();
	}

	private void init() {
		mMainFrame = new JFrame(mName);
		mMainFrame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent e) {
				mContext.connect();
			}

			@Override
			public void windowClosing(WindowEvent e) {
				mContext.disconnect();
			}
		});
		mMainFrame.setSize(mWidth, mHeight);
		mScnWidth = mMainFrame.getToolkit().getScreenSize().width;
		mScnHeight = mMainFrame.getToolkit().getScreenSize().height;
		mMainFrame.setLocation((mScnWidth - mWidth) / 2,
				(mScnHeight - mHeight) / 2);
		mMainFrame.getContentPane().setLayout(new BorderLayout(0, 0));

		JLabel invisibleLabel1 = new JLabel("");
		mMainFrame.getContentPane().add(invisibleLabel1, BorderLayout.NORTH);

		mSessionTextArea = new JTextArea();
		mSessionTextArea.setEditable(false);
		JScrollPane sessionSP = new JScrollPane(mSessionTextArea);
		mMainFrame.getContentPane().add(sessionSP, BorderLayout.EAST);

		BorderLayout bl_messagePanel = new BorderLayout();
		bl_messagePanel.setVgap(10);
		JPanel messagePanel = new JPanel(bl_messagePanel);
		mMainFrame.getContentPane().add(messagePanel, BorderLayout.SOUTH);
		mTextArea = new JTextArea();
		mTextArea.setRows(5);
		JScrollPane textSP = new JScrollPane(mTextArea);
		messagePanel.add(textSP, BorderLayout.CENTER);
		JButton sendButton = new JButton("send");
		sendButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				sendMessage();
			}
		});
		sendButton.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					sendMessage();
				}
			}
		});
		messagePanel.add(sendButton, BorderLayout.EAST);

		JLabel invisibleLabel2 = new JLabel("");
		messagePanel.add(invisibleLabel2, BorderLayout.NORTH);
	}

	public String getmName() {
		return mName;
	}

	public void setmName(String mName) {
		this.mName = mName;
	}

	public void show() {
		mMainFrame.setVisible(true);
	}

	public boolean isShowing() {
		return mMainFrame.isShowing();
	}

	public void setTitle(String title) {
		mMainFrame.setTitle(title);
	}

	public void printMessage(String msg) {
		mSessionTextArea.append(new Date().toString() + " : " + mName + "\n");
		mSessionTextArea.append(msg + "\n");
	}

	private void sendMessage() {
		String msg = mTextArea.getText();
		if (StringUtils.isNotBlank(msg)) {
			mTextArea.setText("");
			mSessionTextArea.append(new Date().toString() + " : me\n");
			mSessionTextArea.append(msg + "\n");
			mContext.chat(msg);
		}
	}
}
